/* 
 * File:   RadianParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 6 juin 2012, 03:47
 */

#include "RadianParameterCommand.h"
#include "model/Context.h"
#include "model/memento/AngleUnitParameterMemento.h"

namespace calculator {

    RadianParameterCommand::RadianParameterCommand(const std::string name) : ParametersCommand(name) {
    }

    RadianParameterCommand::RadianParameterCommand(const RadianParameterCommand& orig) : ParametersCommand(orig._name) {
    }

    RadianParameterCommand::~RadianParameterCommand() {
    }

    RadianParameterCommand* RadianParameterCommand:: clone() const {
        return new RadianParameterCommand(_name);
    }

    void RadianParameterCommand::apply(Parameters* parameters) const {
        parameters->setAngleUnit(Parameters::RADIAN);
    }
    
    const Memento* RadianParameterCommand::createMemento() const throw (CommandException) {
        return new AngleUnitParameterMemento(clone(), Context::getInstance()->getParameters()->getAngleUnit());
    }

    void RadianParameterCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const AngleUnitParameterMemento* angleUnitParameterMemento = dynamic_cast<const AngleUnitParameterMemento*> (memento);
        if (angleUnitParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : AngleUnitParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setAngleUnit(angleUnitParameterMemento->_angleUnit);
    }
}